import {Observer} from "@/utils/observer/Observer";
import {UserRoomApi} from "@/api/user_api";

/**
 * 过号组件 公共业务抽象
 */
class RoomOverObserverBiz extends Observer{
  constructor(self, deviceInfo = {}, item) {
    super();
    // 外部组件
    this.catcheInfo = self;
    // 设备信息
    this.deviceInfo = deviceInfo;
    // 当前组件信息
    this.currentInfo = item || {};
  }


  /**
   * 消息监听入口
   * @param bizType
   * @param msgContent
   */
  async update(bizType, msgContent) {
    let self = this.catcheInfo;
    try {
      switch (bizType) {
        case "doctorLogin":
          await this.doctorLogin(msgContent);
          break;
        case "doctorLogout":
          await this.doctorLogout(msgContent);
          break;
        case "refreshMsg":
          await this.refreshMsg(msgContent);
          break;
      }
    } catch (e) {
      console.error("消息数据加载失败：" + msgContent)
      self.handleException("sdkBizServices", "安卓消息消费异常 -- " + bizType + " -- >", JSON.stringify(e.stack));
    }
  }
  /**
   * 医生登录
   * @param obj
   */
  async doctorLogin(obj) {
    await this.getPassData();
  }


  /**
   * 医生退出
   * @param obj
   */
  async doctorLogout(obj) {
    await this.getPassData();
  }

  /**
   * 刷新消息
   * @param obj
   */
  async refreshMsg(obj) {
    await this.getPassData();
  }

  /**
   * 获取过号数据
   */
  async getPassData(workerIdList = []) {
    // 过号数据
    try {
      let self = this.catcheInfo
      const formData = {
        terminalId: self.deviceInfo.screenId,
        terminalType: self.deviceInfo.screenType || 3,
        businessType: self.deviceInfo.businessType || 3,
        tenantId: self.deviceInfo.hospitalId || "000000",
        type: "OVER",
        dataSource: this.apiType || "0",
        ruleType: self.deviceInfo.ruleType || '0'
      };
      // 接口类型 0表示门诊 1 第三方 2 其他
      if ("1" === self.apiType) {
        formData.workerIdList = workerIdList;
      }
      const {data, code} = await UserRoomApi.getPassData(formData);
      if (code === 200) {
        // 更新过号数据
        this.passNumSetDataList(data || []);
      }
    } catch (e) {
      console.error(e);
    }
  }

  /**
   * 设置过号数据
   */
  passNumSetDataList(data){
    let self = this.catcheInfo;
    try {
      // 获取刷新过号的KEY
      const formRef = self.$refs['vFormRef'];
      if (formRef) {
        formRef.executeChildMethod(this.currentInfo?.id, 'passPatientInfo', data);
      }

    } catch (e) {
      self.handleException('sdkBizServices', '内部组件异常 -- passPatientInfo -- >', JSON.stringify(e.stack));
    }
  }




}

export {RoomOverObserverBiz};
